
'''
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''

'''
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.
    
    To request the provided software under a different license you can contact us at
    support@xoronos.com
'''

#####################################################
##                   Introduction                  ##
#####################################################
## This code example shows how to encrypt a file   ##
## without a secret key. The algorithm generates   ##
## two files: a challenge file ( the encrypted     ##
## file ) and a solution file ( the secret used to ##
## decrypt the challenge file ).                   ##
##                                                 ##
## The algorithm is a stream cypher algorithm so   ##
## all the input/output file can be                ##
## encrypted/decrypted.                            ##
##                                                 ##
## Furthermore, data integrity checks are included ##
## in the encryption algorithm.                    ## 
#####################################################

import xrnlib256 as xrn

def xrn_split_wrapper_example ( ) :

    ##########################
    ## Variable definitions ##
    ##########################

    # library run time settings

    settings = xrn.xrn_crypto_extra_settings_t ()
    # all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    ##########################
    ## Initialize variables ##
    ##########################

    # Initialize default run time settings

    if ( xrn.XSUCCESS != xrn.xrn_load_default_settings( settings ) ) :
        return -1

    # Default settings can be changed, for example
    # settings.crc_bytes = 1 
    # settings.cnt_bytes = 8 
    # settings.state_size = 122 
    # strcpy(settings.rnd_str,"my_string") 

    input_fp = xrn.fopen( "lorem_ipsum.txt", "rb" )
    output_challenge_fp = xrn.fopen( "challenge.enc", "wb" )
    output_solution_fp = xrn.fopen( "solution.enc", "wb" )

    if ( ( input_fp == None ) or ( output_solution_fp == None )
         or ( output_challenge_fp == None ) ) :
        return -1

    ## to know the number of bytes of plain_text in each block you can run
    ## int bytes_in_block =  xrn_estimate_decrypted_block_bytes_length( settings )

    if ( xrn.XSUCCESS != xrn.xrn_split_wrapper( input_fp,        # input file
                                               output_challenge_fp,     # encrypted challenge file ( larger encrypted file )
                                               output_solution_fp,      # solution file ( smaller file used to decrypt challenge file )
                                               settings # runtime settings
          ) ) :

        xrn.fclose( input_fp )
        xrn.fclose( output_challenge_fp )
        xrn.fclose( output_solution_fp )
        return -1

    if ( ( 0 != xrn.fclose( input_fp ) ) or ( 0 != xrn.fclose( output_challenge_fp ) ) or ( 0 != xrn.fclose( output_solution_fp ) ) ) :
        return -1

    # An alternative way to perform the same computation can be achieved using the xrn_split_stream function
    # if ( xrn.XSUCCESS != xrn.xrn_split_stream( input_fp, 
    #                                  output_challenge_fp, 
    #                                  output_solution_fp, 
    #                                  settings
    #       ) ) :

    return 0

def xrn_join_wrapper_example () :

    ##########################
    ## Variable definitions ##
    ##########################

    # library run time settings

    settings = xrn.xrn_crypto_extra_settings_t ()
    # all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    ##########################
    ## Initialize variables ##
    ##########################

    # Initialize default run time settings

    if ( xrn.XSUCCESS != xrn.xrn_load_default_settings( settings ) ) :
        return -1

    # Default settings can be changed, for example
    # settings.crc_bytes = 1 
    # settings.cnt_bytes = 8 
    # settings.state_size = 122 
    # strcpy(settings.rnd_str,"my_string") 

    input_challenge_fp = xrn.fopen( "challenge.enc", "rb" )
    input_solution_fp = xrn.fopen( "solution.enc", "rb" )
    output_decrypted_fp = xrn.fopen( "decrypted_file.txt", "wb" )

    if ( ( input_challenge_fp == None ) or ( input_solution_fp == None ) 
         or ( output_decrypted_fp == None ) ) :
        return -1

    # to know the number of bytes of plain_text in each block you can run
    # int bytes_in_block =  xrn_estimate_decrypted_block_bytes_length( settings )

    if ( xrn.XSUCCESS != xrn.xrn_join_wrapper( input_challenge_fp,       # input file pointer challenge ( big file )
                                              input_solution_fp,        # input file pointer solution ( smaller file )
                                              output_decrypted_fp,      # decrypted file
                                              settings  # runtime settings
          ) ) :

        xrn.fclose( input_challenge_fp )
        xrn.fclose( input_solution_fp )
        xrn.fclose( output_decrypted_fp )
        return -1

    if ( ( 0 != xrn.fclose( input_challenge_fp ) ) or ( 0 != xrn.fclose( input_solution_fp ) )
         or ( 0 != xrn.fclose( output_decrypted_fp ) ) ) :
        return -1

    # An alternative way to perform the same computation can be achieved using the xrn_join_stream function
    # if ( XSUCCESS != xrn_join_stream( input_challenge_fp,
    #                                 input_solution_fp,
    #                                 output_decrypted_fp,
    #                                 settings
    #       ) ) :

    return 0


#############
## Logging ##
#############

# Set the default logging file streams
# errors -> stderr
# warnings -> stdout
# notifications -> stdout
# debug -> stdout

xrn.xrn_set_default_log(  )

# To change the default logging you can use the functions below,
# where fp is the opened file pointer
# 
# xrn_change_error_log( fp )
# xrn_change_warning_log( fp )
# xrn_change_notification_log( fp )
# xrn_change_debug_log( fp )

print( "encryption... " )
if ( 0 != xrn_split_wrapper_example(  ) ) :
    print( "error in encryption " )
    exit( -1 )

print( "decryption... " )
if ( 0 != xrn_join_wrapper_example(  ) ) :
    printf( "error in decryption" )
    exit( -1 )

print( "decrypted file in cache/examples/exec/decrypted_file.txt" )

exit( 0 )



